package com.kfm.hotel.util;

import cn.hutool.core.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Component
public class SendEmailUtil {
    @Autowired//自动装配
    JavaMailSenderImpl mailSender;//实施邮件发送的对象
    @Value("${spring.mail.username}")
    String from;


    public void send(String to, String subject, String content) throws MessagingException {
        //邮件设置1：一个简单的邮件
        MimeMessage mimeMessage=mailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper= new MimeMessageHelper(mimeMessage,true);
        mimeMessageHelper.setSubject(subject);//设置标题
        mimeMessageHelper.setText(content, true);//填入文本内容

        mimeMessageHelper.setTo(to);//接收邮件的一方，可以设置成自己的邮箱测试
        mimeMessageHelper.setFrom(from);//发送邮件的一方邮箱
        mailSender.send(mimeMessage);//发送
    }

    public void vaildEmail(String to, String code) throws MessagingException {
        send(to, "激活邮箱", "【开发喵酒店管理系统】 本次您的验证码为： <span style='font-size:24px;color:red'>" + code + "</span>, 请保管好验证码!");
    }

    public String vaildEmail(String to) throws MessagingException {
        String code = RandomUtil.randomNumbers(6);
        send(to, "激活邮箱", "【开发喵酒店管理系统】 本次您的验证码为： <span style='font-size:24px;color:red'>" + code + "</span>, 验证码 5 分钟内有效，请保管好验证码!");
        return code;
    }


    public static void main(String[] args) {
        System.out.println(RandomUtil.randomNumbers(6));
    }
}
